//
//  posterCollectionViewCell.m
//  HuiWenMovie
//
//  Created by niceDay on 15/7/24.
//  Copyright (c) 2015年 computer. All rights reserved.
//

#import "posterCollectionViewCell.h"
#import "UIViewExt.h"
#import "UIImageView+WebCache.h"
@implementation posterCollectionViewCell

- (id)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        [self _createSubviews];
    }
    return self;
}

- (void)_createSubviews{
    CGFloat imageWidth = self.width*0.9;
    CGFloat imageHeight = self.height *0.9;
    
    NSBundle *bundle = [NSBundle mainBundle];
    NSArray *array = [bundle loadNibNamed:@"PosterDetialView" owner:self options:nil];
    _posterDetailView = [array lastObject];
    _posterDetailView.hidden = YES;
    _posterDetailView.frame =CGRectMake((self.width-imageWidth)/2, (self.height-imageHeight)/2, imageWidth , imageHeight);
    [self.contentView addSubview:_posterDetailView];
    
    
    _imageView = [[UIImageView alloc] initWithFrame:CGRectMake((self.width-imageWidth)/2, (self.height-imageHeight)/2, imageWidth , imageHeight)];
    _imageView.hidden = NO;
    [self.contentView addSubview:_imageView];

    
}

-(void)setModal:(messageModal *)modal{
    _modal = modal;
    _posterDetailView.modal = modal;
    
    [self setNeedsLayout];
}

- (void)flipView{
    if (_isBack == YES) {
        _imageView.hidden = YES;
        _posterDetailView.hidden = NO;
    }
    else{
        _imageView.hidden = NO;
        _posterDetailView.hidden = YES;
    }
    [self _flipView:self isLeft:_imageView.hidden];
    _isBack = !_isBack;
    _imageView.hidden = !_imageView.hidden;
    _posterDetailView.hidden = !_posterDetailView.hidden;
    
    
}

- (void)_flipView:(UIView *)flipView isLeft:(BOOL)isLeft{
    UIViewAnimationOptions option = isLeft?UIViewAnimationOptionTransitionFlipFromLeft:UIViewAnimationOptionTransitionFlipFromRight;
    [UIView transitionWithView:flipView duration:.3 options:option animations:nil completion:nil];
}



- (void)layoutSubviews{
    [super layoutSubviews];
     [_imageView sd_setImageWithURL:[NSURL URLWithString:_modal.images[@"large"]]];
}
@end
